<template>
  <div
    class="playlist-container"
    v-loading="loading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
  >
    <vue-scroll :ops="ops" ref="vs">
      <div class="top-wrap">
        <div class="topimg-wrap">
          <!-- 封面 -->
          <img :src="playlist.coverImgUrl" alt />
        </div>
        <div class="info-wrap">
          <!-- 名字 -->
          <p class="title">{{ playlist.name }}</p>
          <div class="author-wrap">
            <!-- 头像 -->
            <img
              class="avatar"
              :src="playlist.creator.avatarUrl"
              v-if="playlist.creator !== undefined"
              alt
            />
            <!-- 名字 -->
            <span class="name" v-if="playlist.creator !== undefined">{{
              playlist.creator.nickname
            }}</span>
            <!-- 时间 -->
            <span class="time">{{ playlist.createTime | dateFormat }}</span>
          </div>
          <div class="play-wrap" @click="playList(playlist.tracks)">
            <span class="iconfont icon-circle-play"></span>
            <span class="text">播放全部</span>
          </div>
          <div class="tag-wrap">
            <span class="title">标签:</span>
            <ul>
              <li v-for="(item, index) in playlist.tags" :key="index">
                {{ item }}
              </li>
            </ul>
          </div>
        </div>
      </div>
      <div class="desc-wrap">
        <span class="title">简介:</span>
        <span class="desc">{{ playlist.description }}</span>
      </div>
      <el-tabs v-model="activeIndex">
        <el-tab-pane label="歌曲列表" name="1">
          <table class="el-table playlit-table">
            <thead>
              <th></th>
              <th></th>
              <th>音乐标题</th>
              <th>歌手</th>
              <th>专辑</th>
              <th>时长</th>
            </thead>
            <tbody>
              <tr
                class="el-table__row"
                v-for="(item, index) in playlist.tracks"
                :key="index"
                @click="playMusic(item)"
              >
                <td style="width:50px">{{ index + 1 }}</td>
                <td>
                  <div class="img-wrap">
                    <img :src="item.al.picUrl + '?param=130y130'" alt />
                    <span class="iconfont icon-play"></span>
                  </div>
                </td>
                <td>
                  <div class="song-wrap">
                    <div class="name-wrap">
                      <!-- 名称 -->
                      <span>{{ item.name }}</span>
                      <!-- mv图标 -->
                      <span
                        v-if="item.mvid !== 0"
                        class="iconfont icon-mv"
                      ></span>
                    </div>
                  </div>
                </td>
                <td>{{ item.ar[0].name }}</td>
                <td>{{ item.al.name }}</td>
                <td>{{ item.dt | formDate }}</td>
              </tr>
            </tbody>
          </table>
        </el-tab-pane>
        <el-tab-pane :label="`评论(${total})`" name="2">
          <!-- 精彩评论 -->
          <div
            class="comment-wrap"
            v-if="hotComments !== undefined && hotComments.length !== 0"
          >
            <p class="title">
              精彩评论
              <span class="number">({{ hotComments.length }})</span>
            </p>
            <div class="comments-wrap">
              <!-- 评论 -->
              <div
                class="item"
                v-for="(item, index) in hotComments"
                :key="index"
              >
                <div class="icon-wrap">
                  <!-- 头像 -->
                  <img :src="item.user.avatarUrl + '?param=50y50'" alt />
                </div>
                <div class="content-wrap">
                  <div class="content">
                    <span class="name">{{ item.user.nickname }}</span>
                    <span class="comment">：{{ item.content }}</span>
                  </div>
                  <!-- 回复 -->
                  <div class="re-content" v-if="item.beReplied.length !== 0">
                    <span class="name">{{
                      item.beReplied[0].user.nickname
                    }}</span>
                    <span class="comment"
                      >：{{ item.beReplied[0].content }}</span
                    >
                  </div>
                  <div class="date">{{ item.time | dateFormat }}</div>
                </div>
              </div>
            </div>
          </div>
          <!-- 最新评论 -->
          <div class="comment-wrap">
            <p class="title">
              最新评论
              <span class="number">({{ total }})</span>
            </p>
            <div class="comments-wrap">
              <!-- 评论 -->
              <div class="item" v-for="(item, index) in comments" :key="index">
                <div class="icon-wrap">
                  <!-- 头像 -->
                  <img :src="item.user.avatarUrl + '?param=50y50'" alt />
                </div>
                <div class="content-wrap">
                  <div class="content">
                    <span class="name">{{ item.user.nickname }}</span>
                    <span class="comment">：{{ item.content }}</span>
                  </div>
                  <!-- 回复 -->
                  <div class="re-content" v-if="item.beReplied.length !== 0">
                    <span class="name">{{
                      item.beReplied[0].user.nickname
                    }}</span>
                    <span class="comment"
                      >：{{ item.beReplied[0].content }}</span
                    >
                  </div>
                  <div class="date">{{ item.time | dateFormat }}</div>
                </div>
              </div>
            </div>
          </div>
          <!-- 分页器 -->
          <div class="pagebox">
            <el-pagination
              @current-change="handleCurrentChange"
              background
              layout="prev, pager, next"
              :total="total"
              :current-page="pageNum"
              :page-size="pageSize"
            ></el-pagination>
          </div>
        </el-tab-pane>
      </el-tabs>
    </vue-scroll>
  </div>
</template>

<script>
import { GetSong, GetListDetail, GetListComments } from "../../api/music"
export default {
  name: "playlist",
  data() {
    return {
      loading: true,
      id: "",
      activeIndex: "1",
      // 总条数
      total: 0,
      // 页码
      pageNum: 1,
      pageSize: 10,

      playlist: {},
      hotComments: [],
      comments: [],
      ops: {
        //vuescroll的配置
        vuescroll: {
          sizeStrategy: "percent",
          detectResize: true,
        },
        scrollPanel: {
          scrollingX: false,
        },
        rail: {},
        bar: {
          showDelay: 500,
          onlyShowBarOnScroll: false,
          keepShow: true,
          background: "#b3c0d1",
          opacity: 1,
          hoverStyle: false,
          specifyBorderRadius: false,
          minSize: false,
          size: "6px",
          disable: false,
        },
      },
    }
  },
  filters: {
    formDate(date) {
      var m = parseInt(date / 1000 / 60)
      if (m < 10) {
        m = "0" + m
      }
      var s = parseInt((date / 1000) % 60)
      if (s < 10) {
        s = "0" + s
      }
      return m + ":" + s
    },
  },
  created() {
    this.id = this.$route.query.id
    this.listdetail()
    this.listcomments()
  },
  methods: {
    handleCurrentChange(val) {
      this.pageNum = val
      this.getPlaylistComments()
    },

    // 点击播放全部按钮触发的事件：将所有歌曲添加到播放列表中
    playList(list, index) {
      this.$store.commit("addlist", list)
    },

    // 获取歌单详情
    async listdetail() {
      const res = await GetListDetail({
        id: this.id,
      })
      this.avatarUrl = res.playlist.creator.avatarUrl
      this.playlist = res.playlist
      this.loading = false
    },

    // 获取歌单评论
    async listcomments() {
      const res = await GetListComments({
        id: this.id,
        limit: this.pageSize,
        offset: (this.pageNum - 1) * this.pageSize,
      })
      this.comments = res.comments
      this.hotComments = res.hotComments
      this.total = res.total
    },

    // 点击播放歌曲
    async playMusic(item) {
      console.log(item)
      this.$store.dispatch("getSong", { id: item.id, autoPlay: true })
    },
  },
}
</script>

<style lang="scss" scoped>
.playlist-container {
  margin: auto;
  padding-top: 40px;
  height: calc(100% - 40px);
  user-select: none;
}

.playlist-container .top-wrap {
  margin: 0px 20px;
  display: flex;
}

.el-tabs {
  margin: 0px 20px;
}

.playlist-container .top-wrap span {
  font-size: 17px;
}
.topimg-wrap {
  width: 190px;
  height: 190px;
  margin-right: 20px;
}
.topimg-wrap img {
  width: 100%;
  height: 100%;
}
.img-wrap {
  width: 80px;
  height: 80px;
  margin-right: 30px;
  overflow: hidden;
  position: relative;
}
tr:hover {
  cursor: pointer;
  color: rgb(98, 158, 218);
}
.img-wrap img {
  width: 80px;
  height: 80px;
}
.img-wrap .icon-play {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 25px;
  height: 25px;
  color: rgb(98, 158, 218);
  font-size: 12px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.8);
  opacity: 0;
  transition: 0.5s;
}
tr:hover .icon-play {
  opacity: 1;
}

.icon-play::before {
  transform: translateX(1px);
}
li {
  list-style: none;
}
.playlist-container .top-wrap .info-wrap {
  width: calc(100%- 240px) !important;
}
.playlist-container .top-wrap .info-wrap .title {
  margin-bottom: 20px;
}

.playlist-container .top-wrap .info-wrap .author-wrap {
  display: flex;
  align-items: center;
  margin-bottom: 25px;
}

.playlist-container .top-wrap .info-wrap .author-wrap img {
  width: 35px;
  height: 35px;
  border-radius: 50%;
  margin-right: 10px;
}

.playlist-container .top-wrap .info-wrap .author-wrap .name {
  margin-right: 10px;
}

.playlist-container .top-wrap .info-wrap .author-wrap .time {
  font-size: 14px;
}

.playlist-container .top-wrap .info-wrap .play-wrap {
  width: 120px;
  height: 35px;
  border-radius: 4px;
  background: linear-gradient(to right, rgb(98, 158, 218), rgb(62, 133, 204));
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 25px;
  transition: 0.5s;
}
.play-wrap:hover {
  cursor: pointer;
  opacity: 0.8;
}
.desc-wrap {
  margin: 0px 20px;
  line-height: 25px;
}
.playlist-container .top-wrap .info-wrap .play-wrap span {
  color: white;
}

.playlist-container .top-wrap .info-wrap .play-wrap span.iconfont {
  margin-right: 8px;
}

.playlist-container .top-wrap .info-wrap .play-wrap span.text {
  font-size: 16px;
}

.playlist-container .top-wrap .info-wrap .tag-wrap {
  display: flex;
  margin-bottom: 15px;
}

.playlist-container .top-wrap .info-wrap .tag-wrap span {
  margin: 0;
}

.playlist-container .top-wrap .info-wrap .tag-wrap ul {
  display: flex;
  align-items: center;
}

.playlist-container .top-wrap .info-wrap .tag-wrap li {
  font-size: 15px;
}

.playlist-container .top-wrap .info-wrap .tag-wrap li:not(:last-child)::after {
  content: "/";
  margin: 0 4px;
}

.playlist-container .top-wrap .info-wrap .desc-wrap span:last-child {
  font-size: 15px;
}

.playlist-container .top-wrap .info-wrap span:first-child {
  margin-right: 10px;
}

.playlist-container .comment-wrap {
  margin-bottom: 30px;
}

.playlist-container .comment-wrap .title {
  font-size: 20px;
}

.playlist-container .comment-wrap .title .number {
  color: black;
  font-size: 18px;
}

.playlist-container .comment-wrap .item {
  display: flex;
  padding-top: 20px;
}

.playlist-container .comment-wrap .item .icon-wrap {
  margin-right: 15px;
}

.playlist-container .comment-wrap .item .icon-wrap img {
  width: 50px;
  height: 50px;
  border-radius: 50%;
}

.playlist-container .comment-wrap .item:not(:last-child) .content-wrap {
  border-bottom: 1px solid #f2f2f1;
}

.playlist-container .comment-wrap .item .date {
  color: #bebebe;
  font-size: 14px;
}

.playlist-container .comment-wrap .item .content-wrap {
  padding-bottom: 20px;
  flex: 1;
}

.playlist-container .comment-wrap .item .content-wrap .name {
  color: #517eaf;
  font-size: 14px;
}

.playlist-container .comment-wrap .item .content-wrap .comment {
  font-size: 14px;
}

.playlist-container .comment-wrap .item .content-wrap .content,
.playlist-container .comment-wrap .item .content-wrap .re-content {
  margin-bottom: 10px;
}

.playlist-container .comment-wrap .item .content-wrap .re-content {
  padding: 10px;
  background-color: #e6e5e6;
  border-radius: 5px;
}
.pagebox {
  display: flex;
  width: 100%;
  height: 60px;
  justify-content: center;
  align-items: center;
}
</style>
